package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public PageInfo<Travel> queryForList(TravelQuery qo) {
        if (qo == null) {
            throw new LogicException("参数错误:");
        }
        // 如果不是已发布的获取不是公开的则判断用户是否登录, 如果登录就查询该用户的游记
        if (qo.getState() != Travel.STATE_RELEASE || qo.getIsPublic() != 1) {
            // TODO
        }
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo<>(travelMapper.selectForList(qo));
    }

    @Override
    public Travel queryDetailById(Long id) {
        if (id == null) {
            throw new LogicException("参数错误:");
        }
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContentById(Long id) {
        if (id == null) {
            throw new LogicException("参数错误");
        }
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> queryDetailTop3(TravelQuery destId) {
        if (destId == null) {
            throw new LogicException("参数错误");
        }
        return travelMapper.selectDetailTop3(destId);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        if (travel == null) {
            throw new LogicException("参数错误");
        }
        if (travel.getAuthor() == null) {
            throw new LogicException("未登录, 请先登录!!!");
        }
        TravelContent content = travel.getTravelContent();
        String contentContent = content.getContent();
        if (contentContent == null) {
            throw new LogicException("没有编写内容!!!");
        }
        travel.setSummary(contentContent.length() > 200 ? contentContent.substring(0, 200) : contentContent);
        travel.setLastUpdateTime(new Date());
        if (travel.getId() == null) {
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            content.setId(travel.getId());
            travelContentMapper.insert(content);
        } else {
            travelMapper.updateByPrimaryKey(travel);
            content.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(content);
        }
        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public List<Travel> queryByAuthorId(Long uid) {
        return travelMapper.selectByAuthorId(uid);
    }

    @Override
    public int getCountByAuthorId(Long uid) {
        return travelMapper.getCountByAuthorId(uid);
    }
}
